home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / LDelayedTask / LDelayedTask.cp next >
Encoding:
Text File  |  1995-08-28  |  4.0 KB  |  154 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //        • LDelayedTask.cp            © 1995, Éric Forget. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    ************************************************************************
  6. //    *                                                                      *
  7. //    *    Before using this code you should read the "License Agreement"     *
  8. //    *    document and agree with it.                                        *
  9. //    *                                                                      *
  10. //    ************************************************************************
  11. //
  12. //    LDelayedTask is an abstract class to execute a task after a certain
  13. //    period of time. This class is not really precise: it use TickCount()
  14. //    as time mesure. On another side, you can do about all you want in the
  15. //    ExecuteSelf() method.
  16. //
  17. //    One example of its use, is to implement a "Find" in a list or table, 
  18. //    a la Metrowerks IDE. Each time the user press a key, you call RestartTask().
  19. //    When the user will not have pressed a key for a determined period, you
  20. //    do the search in the ExecuteSelf() method.
  21. //
  22. // ---------------------------------------------------------------------------
  23. //
  24. //    Instruction Notes:
  25. //    -----------------
  26. //
  27. //    1) Derive this class (perhaps in mixin) and override ExecuteSelf();
  28. //
  29. //    2) In ExecuteSelf() you should return whether you want to continue
  30. //       to be called after the next interval.
  31. //
  32. // ---------------------------------------------------------------------------
  33.  
  34. #include    "LDelayedTask.h"
  35.  
  36. #include    "UDelayedTaskQueue.h"
  37.  
  38.  
  39. // ---------------------------------------------------------------------------
  40. //        • LDelayedTask
  41. // ---------------------------------------------------------------------------
  42.  
  43. LDelayedTask::LDelayedTask(
  44.     Int32    inFirstDelay,
  45.     Int32    inNextDelay,
  46.     Boolean inDeleteOnCompletion)
  47.         
  48.         : LTask(inFirstDelay, inNextDelay, inDeleteOnCompletion)
  49. {
  50. }
  51.  
  52.  
  53. // ---------------------------------------------------------------------------
  54. //        • ~LDelayedTask
  55. // ---------------------------------------------------------------------------
  56.  
  57. LDelayedTask::~LDelayedTask()
  58. {
  59.     
  60. }
  61.  
  62.  
  63. // ---------------------------------------------------------------------------
  64. //        • StartTask
  65. // ---------------------------------------------------------------------------
  66.  
  67. void
  68. LDelayedTask::StartTask()
  69. {
  70.     if(!IsExecuting()) {
  71.         
  72.         LTask::StartTask();
  73.         
  74.         UDelayedTaskQueue::AddTask(this);
  75.         
  76.         mStartTime = ::TickCount();
  77.         mIsFirstTask = true;
  78.     }
  79. }
  80.  
  81.  
  82. // ---------------------------------------------------------------------------
  83. //        • StopTask
  84. // ---------------------------------------------------------------------------
  85.  
  86. void
  87. LDelayedTask::StopTask()
  88. {
  89.     if(IsExecuting()) {
  90.     
  91.         UDelayedTaskQueue::RemoveTask(this);
  92.         
  93.         LTask::StopTask();
  94.     }
  95. }
  96.  
  97.  
  98. // ---------------------------------------------------------------------------
  99. //        • ContinueTask
  100. // ---------------------------------------------------------------------------
  101.  
  102. void
  103. LDelayedTask::ContinueTask()
  104. {
  105.     mIsFirstTask = false;
  106.     mStartTime = ::TickCount();
  107. }
  108.  
  109.  
  110. // ---------------------------------------------------------------------------
  111. //        • StartTaskSelf
  112. // ---------------------------------------------------------------------------
  113. //    For a DelayedTask, it is not necessary to pre allocate all the stuff!
  114.  
  115. void
  116. LDelayedTask::StartTaskSelf()
  117. {
  118. }
  119.  
  120.  
  121. // ---------------------------------------------------------------------------
  122. //        • StopTaskSelf
  123. // ---------------------------------------------------------------------------
  124.  
  125. void
  126. LDelayedTask::StopTaskSelf()
  127. {
  128. }
  129.  
  130.  
  131. // ---------------------------------------------------------------------------
  132. //        • IsItTimeToExecute
  133. // ---------------------------------------------------------------------------
  134.  
  135. Boolean
  136. LDelayedTask::IsItTimeToExecute()
  137. {
  138.     Int32        currentTime = ::TickCount();
  139.     Boolean        rval = false;
  140.     
  141.     
  142.     if(mIsFirstTask) {
  143.     
  144.         rval = (currentTime >= (mFirstDelay + mStartTime));
  145.     
  146.     } else {
  147.     
  148.         rval = (currentTime >= (mNextDelay + mStartTime));
  149.     }
  150.     
  151.     return rval;
  152. }
  153.  
  154.